//
//  WeiBoTableViewCell.m
//  CloudWeibo
//
//  Created by CLOUD on 11/26/14.
//  Copyright (c) 2014 Cloud. All rights reserved.
//

#import "WeiBoTableViewCell.h"
#import "WeiBoModel.h"
#import "WeiBoView.h"
#import "UIImageView+WebCache.h"
#import "UIUtils.h"
#import "RegexKitLite.h"
#import "CloudImageView.h"
#import "UserViewController.h"

@implementation WeiBoTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self _initView];
    }
    return self;
}

- (void) _initView
{
    _userImage = [[CloudImageView alloc] initWithFrame:CGRectZero];
    _userImage.backgroundColor = [UIColor clearColor];
    _userImage.layer.cornerRadius = 5;
    _userImage.layer.borderWidth = .5;
    _userImage.layer.borderColor = [UIColor grayColor].CGColor;
    _userImage.layer.masksToBounds = YES;
    [self.contentView addSubview:_userImage];
    
    _nickLable = [[UILabel alloc] initWithFrame:CGRectZero];
    _nickLable.backgroundColor = [UIColor clearColor];
    _nickLable.font = [UIFont systemFontOfSize:14.0];
    [self.contentView addSubview:_nickLable];
    
    _sourceLable = [[UILabel alloc] initWithFrame:CGRectZero];
    _sourceLable.backgroundColor = [UIColor clearColor];
    _sourceLable.font = [UIFont systemFontOfSize:12.0];
    _sourceLable.textColor = [UIColor grayColor];
    [self.contentView addSubview:_sourceLable];
    
    _createLable = [[UILabel alloc] initWithFrame:CGRectZero];
    _createLable.backgroundColor = [UIColor clearColor];
    _createLable.font = [UIFont systemFontOfSize:12.0];
    _createLable.textColor = [UIColor grayColor];
    [self.contentView addSubview:_createLable];
    
    _weiBoView = [[WeiBoView alloc] initWithFrame:CGRectZero];
    [self.contentView addSubview:_weiBoView];
    
    UIView *selectedView = [[UIView alloc] initWithFrame:CGRectZero];
    selectedView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"statusdetail_cell_sepatator"]];
    self.selectedBackgroundView = selectedView;
    [selectedView release];
}


- (void) setWeiBoModel:(WeiBoModel *)weiBoModel
{
    if (_weiBoModel != weiBoModel) {
        [_weiBoModel release];
        _weiBoModel = [weiBoModel retain];
        
    }
    
    __block WeiBoTableViewCell *this = self;
    _userImage.touchBlock = ^{
        NSString *nickName = this.weiBoModel.user.screen_name;
        UserViewController *userViewController = [[UserViewController alloc] init];
        userViewController.userName = nickName;
        [this.getViewController.navigationController pushViewController:userViewController animated:YES];
        [userViewController release];
    };
}

- (void) layoutSubviews
{
    [super layoutSubviews];
    _userImage.frame = CGRectMake(5, 5, 35, 35);
    NSString *userImageUrl = _weiBoModel.user.profile_image_url;
    [_userImage setImageWithURL:[NSURL URLWithString:userImageUrl]];
    
    _nickLable.frame = CGRectMake(50, 5, 200, 20);
    _nickLable.text = _weiBoModel.user.screen_name;
    
    _weiBoView.weiBoModel = _weiBoModel;
    /*****
     
     此高度必须发生变化。如果不变化，会导致数据重复。
     
     *****/
    float height = [WeiBoView getWeiboViewHeight:_weiBoModel isRepost:NO isDetail:NO];
    _weiBoView.frame = CGRectMake(50, _nickLable.bottom + 10, kWeibo_Width_List, height);
    
    
    NSString *createDate = _weiBoModel.createDate;
    if (createDate != nil) {
        _createLable.hidden = NO;
        _createLable.text = [UIUtils fomateString:createDate];
        _createLable.frame = CGRectMake(50, self.height-23, 100, 20);
        [_createLable sizeToFit];
    }else{
        _createLable.hidden = YES;
    }
    
    
    NSString *source = _weiBoModel.source;
    NSString *newSource = [self parseSource:source];
    if (newSource != nil) {
        _sourceLable.hidden = NO;
        _sourceLable.text = [NSString stringWithFormat:@"来自%@",newSource];
        _sourceLable.frame = CGRectMake(_createLable.right + 8, self.height - 23, 100, 20);
        [_sourceLable sizeToFit];
    }else{
        _sourceLable.hidden = YES;
    }
    
    
    
}

- (NSString *) parseSource:(NSString *) source
{
    NSString *regex = @">.+<";
    NSArray *arr = [source componentsMatchedByRegex:regex];
    if (arr.count > 0) {
        NSString *newSource = [arr objectAtIndex:0];
        NSRange range;
        range.location = 1;
        range.length = newSource.length - 2;
        NSString *result = [newSource substringWithRange:range];
        return result;
    }
    return nil;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
